package com.liangtian.mdm.system.file.service.impl;

import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.dm.common.enums.Flag;
import com.liangtian.mdm.system.file.dao.SysFileDao;
import com.liangtian.mdm.system.file.model.SysFile;
import com.liangtian.mdm.system.file.service.SysFileService;
import com.liangtian.mdm.system.parameter.service.SysParameterService;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/9/25.
 */
@Service
public class SysFileServiceSuper implements SysFileService {
    @Autowired
    SysFileDao sysFileDao;
    @Autowired
    SysParameterService sysParameterService;

    /**
     * 插入附件信息
     *
     * @param fileName
     * @param path
     * @param fileSize
     * @param surveyType
     * @param dataId
     * @param keyColumn
     * @param operType
     */
    public void insertFileInfo(String fileName, String path, long fileSize, String surveyType, String dataId, String keyColumn, String operType) {
        //获取列名
        SysFile sysFile = new SysFile();
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        sysFile.setCreator(String.valueOf(user.getUserId()));
        sysFile.setFileName(fileName);
        sysFile.setFileType(fileName.substring(fileName.lastIndexOf(".")));
        sysFile.setFilePath(path);
        sysFile.setFileSize(fileSize + "");
        sysFile.setSurveyType(surveyType);
        sysFile.setDataId(dataId);
        sysFile.setFileColumn(keyColumn);//--把列名保存进去
        sysFile.setOperType(operType);
        sysFileDao.insertSysFile(sysFile);
    }


    public Integer charCount(String key, char c) {
        char[] chars = key.toCharArray();
        int count = 0;
        for (int i = 0; i < chars.length; i++) {
            if (c == chars[i])
                count++;
        }
        return count;
    }

    @Override
    public Map<String, Object> insertFile(HttpServletRequest request, Map<String, Object> Fcond) {
        return null;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo queryFilesPageBySurvey(Map<String, Object> param) {
        PageInfo<List<SysFile>> pageInfo = null;
        try {
            Object pNo = param.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(param.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            String operType = (String) param.get("operType");
            if (Flag.OPER_2.getDesc().equals(operType))
                param.put("operType", Flag.OPER_2.getCode());//审核
            else if (Flag.OPER_1.getDesc().equals(operType))
                param.put("operType", Flag.OPER_1.getCode());//变更
            else if (Flag.OPER_3.getDesc().equals(operType))
                param.put("operType", Flag.OPER_3.getCode());//冻结
            else if (Flag.OPER_4.getDesc().equals(operType))
                param.put("operType", Flag.OPER_4.getCode());//解冻
            else if ("list".equals(operType))
                param.put("operType", "apply");
            List<SysFile> filesList = sysFileDao.queryFileBySurvey(param);
            pageInfo = new PageInfo(filesList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<SysFile> queryFileBySurvey(Map<String, Object> param) {
        return sysFileDao.queryFileBySurvey(param);
    }

    @Override
    public SysFile queryFileById(String id) {
        return sysFileDao.queryFileById(id);
    }

    @Override
    public void delFileByPath(SysFile sFile) {
    }

    @Override
    public void delFileByPath(List<SysFile> fileLst) {

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo getCodeXzFjById(Map<String, Object> map) {
        PageInfo<List<SysFile>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）
            List<SysFile> list = sysFileDao.queryFileBySurvey(map);
            pageInfo = new PageInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<SysFile> queryFileByCond(Map<String, Object> cond) {
        return sysFileDao.queryFileByCond(cond);
    }

    @Override
    public List<SysFile> queryFilesBySurvey(Map<String, Object> param) {
        return sysFileDao.queryFilesBySurvey(param);
    }

    @Override
    public Map<String, Object> insertSysContentFile(HttpServletRequest request, Map<String, Object> Fcond) {
        return null;
    }
}
